<?php

namespace App\Backend\Service;

use App\Models\Doubanpost;
use App\Exceptions\InvalidRequestException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Backend\Service\BaseService;
class DoubanpostService extends BaseService
{

    private $model;

    public function __construct(Doubanpost $model)
    {
        parent ::__construct();
        $this->model = $model;
    }

     /**
     * 列表
     */
    public function getList($request,$data=[])
    {
        $page = intval($request->input('page')) ?$request->input('page') :$this->cpage;
        $limit = intval($request->input('limit')) ?$request->input('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        //dd($data);
        $where = [];
        if($data['title']){
            $where[]=['title','like','%'.$data['title'].'%'];
        }

        if($data['start_date']){
            $where[]=['update_time','>=',$data['start_date']];
        }
        if($data['end_date']){
            $where[]=['update_time','<=',$data['end_date']];
        }
        
        if($data['check_status']){
            $where[]=['check_status','=',$data['check_status']];
        }

        if($data['check_result']){
            $where[]=['check_result','=',$data['check_result']];
        }

        if($data['channel']){
            $where[]=['channel','=',$data['channel']];
        }

        //dd($where);
        $count = $this->model->where($where)->count();
        $fields = ['*'];
        $list = $this->model->select($fields)
        ->where($where)
        ->offset($offset)->limit($limit)
        ->orderBy('id', 'desc')->get()->toArray();
        foreach ($list as  &$v){
            $channel=$v['channel'];
            
            $v['content_books'] =json_decode($v['content_books'],true);
            $v['images'] =json_decode($v['images'],true);   
            $v['guanlian_shop'] =json_decode($v['guanlian_shop'],true);   
        }
       
        $result = [
            'page'=> $page,
            'limit'=> $limit,
            'total_page'=> ceil($count/$limit),
            'count'=>$count,
            'data'=> $list
        ];
        return $result;
    }

    /**
     * 详情
     */

    public function detail($request,$unique_id){
       $where = [
           ['unique_id','=',$unique_id],
        ];
        $list = $this->model->where($where)->first();;
        if(!empty($list)){
            $list['content_books'] =json_decode($list['content_books'],true);
            $list['images'] =json_decode($list['images'],true);   
            $list['guanlian_shop'] =json_decode($list['guanlian_shop'],true);   
        }

        
        return $list;
    }
      /**
     * 添加
     */
    public function add($request,$data)
    {
        $data['updator'] = $data['creator']=$this->userId;
        $res =  $this->model->insert($data);
        if($res){
            $this->success();
        }else{
            $this->error('添加失败');
        }

    }

    /**
     * 编辑
     */
    public function edit($request,$data,$unique_id)
    {
        $res = $this->model->where('unique_id',$unique_id)->first();
        if(!$res) {
            $this->error('内容不存在');
        }
        $check_result=$data['check_result'];
        //同意 check_status
        if($check_result=='1'){
            $data['status']='1';
            $data['check_status']='3';

        }
        //拒绝
        if($check_result=='3'){
            $data['status']='2';
            $data['check_status']='3';


        }
        $data['update_time'] = date("Y-m-d H:i:s");

        $res =  $this->model->where('unique_id',$unique_id)->update($data);
        if(!$res){
            $this->error('修改失败');
        }else{
            $this->success();
        }
    }   

     /**
     * 删除
     */
    public function del($ids)
    {
        $res =  $this->model->whereIn('unique_id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   
    


}
